package org.example.aidemo.service.security;


import org.example.aidemo.mapper.UserMapper;
import org.example.aidemo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) {
        User userInfo = userMapper.findByUsername(username);
        if (userInfo == null) {
            throw new UsernameNotFoundException("User not found: " + username);
        }
        return org.springframework.security.core.userdetails.User.builder()
                .username(userInfo.getUsername())
                .password(userInfo.getPasswordHash())
                .accountLocked(false) // 确保账户未被锁定
                .build();
    }
}
